package com.define.commons.query.service.impl;

import com.define.commons.query.service.CommonMapperBaseService;
import com.define.commons.query.core.Query;
import com.define.commons.query.core.dao.CommonMapperBaseDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 根据泛型提供基础CRUD的 ServiceImpl
 * 使用 CommonMapper 实现
 * @Author xiewenlong
 * @Date 2018/10/31 12:49
 */
public class CommonMapperServiceImpl<T> implements CommonMapperBaseService<T> {

    @Autowired
    private CommonMapperBaseDao<T> mapper;

    @Override
    public int deleteByPrimaryKey(Object key) {
        return mapper.deleteByPrimaryKey(key);
    }

    @Override
    public int delete(T record) {
        return mapper.delete(record);
    }

    @Override
    public int insert(T record) {
        return mapper.insert(record);
    }

    @Override
    public int insertSelective(T record) {
        return mapper.insertSelective(record);
    }

    @Override
    public boolean existsWithPrimaryKey(Object key) {
        return mapper.existsWithPrimaryKey(key);
    }

    @Override
    public List<T> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public T selectByPrimaryKey(Object key) {
        return mapper.selectByPrimaryKey(key);
    }

    @Override
    public int selectCount(T record) {
        return mapper.selectCount(record);
    }

    @Override
    public List<T> select(T record) {
        return mapper.select(record);
    }

    @Override
    public T selectOne(T record) {
        return mapper.selectOne(record);
    }

    @Override
    public int updateByPrimaryKey(T record) {
        return mapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(T record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByExample(Object example) {
        return mapper.deleteByExample(example);
    }

    @Override
    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }

    @Override
    public int selectCountByExample(Object example) {
        return mapper.selectCountByExample(example);
    }

    @Override
    public T selectOneByExample(Object example) {
        return mapper.selectOneByExample(example);
    }

    @Override
    public int updateByExample(T record, Object example) {
        return mapper.updateByExample(record, example);
    }

    @Override
    public int updateByExampleSelective(T record, Object example) {
        return mapper.updateByExampleSelective(record, example);
    }

    @Override
    public List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds) {
        return mapper.selectByExampleAndRowBounds(example, rowBounds);
    }

    @Override
    public List<T> selectByRowBounds(T record, RowBounds rowBounds) {
        return mapper.selectByRowBounds(record, rowBounds);
    }

    @Override
    public int insertList(List<? extends T> recordList) {
        return mapper.insertList(recordList);
    }

    @Override
    public int insertUseGeneratedKeys(T record) {
        return mapper.insertUseGeneratedKeys(record);
    }

    @Override
    public PageInfo<T> getPage(Query query) {
        PageHelper.startPage(query.getPage(), query.getPageSize(), query.getSorts());
        List<T> list = mapper.select((T) query.getParams());
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<T> getPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<T> list = mapper.selectAll();
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<T> getPage(T t, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<T> list = mapper.select(t);
        return new PageInfo<>(list);
    }

}
